package org.example.lotterysystem.controller;

import org.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import org.example.lotterysystem.common.exception.ControllerException;
import org.example.lotterysystem.common.pojo.CommonResult;
import org.example.lotterysystem.common.utils.JacksonUtil;
import org.example.lotterysystem.controller.param.ShortMessageLoginParam;
import org.example.lotterysystem.controller.param.UserPasswordLoginParam;
import org.example.lotterysystem.controller.param.UserRegisterParam;
import org.example.lotterysystem.controller.result.*;
import org.example.lotterysystem.service.UserService;
import org.example.lotterysystem.service.VerificationCodeService;
import org.example.lotterysystem.service.dto.UserDTO;
import org.example.lotterysystem.service.dto.UserLoginDTO;
import org.example.lotterysystem.service.dto.UserRegisterDTO;
import org.example.lotterysystem.service.dto.UserSendCodeDTO;
import org.example.lotterysystem.service.enmus.UserIdentityEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger logger= LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private VerificationCodeService verificationCodeService;
    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> userRegister(@Validated @RequestBody UserRegisterParam  param){
        logger.info("userRegister UserRegisterParam:{}", JacksonUtil.writeValueAsString(param));
        //调用Service层
        UserRegisterDTO userRegisterDTO=userService.register(param);
        return CommonResult.success(convertUserRegisterParam(userRegisterDTO));
    }

    /**
     * 这里将从service层拿到的数据进行转换
     * @param userRegisterDTO
     * @return
     */
    private UserRegisterResult convertUserRegisterParam(UserRegisterDTO userRegisterDTO) {
        UserRegisterResult userRegisterResult=new UserRegisterResult();
        if(null==userRegisterDTO){
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
       userRegisterResult.setUserId(userRegisterDTO.getId());
        return userRegisterResult;
    }
    private UserLoginResult convertUserLoginParam(UserLoginDTO userLoginDTO) {
        UserLoginResult userLoginResult=new UserLoginResult();
        if(null==userLoginResult){
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
        userLoginResult.setToken(userLoginDTO.getToken());
        userLoginResult.setIdentity(userLoginResult.getIdentity());
        return userLoginResult;
    }
    /**
     *
     * 补充方法,没有调用
     * @param userSendCodeDTO
     * @return
     */
    private UserLoginResult convertUserLoginParam(UserSendCodeDTO userSendCodeDTO) {
        UserLoginResult userLoginResult=new UserLoginResult();
        if(null==userSendCodeDTO){
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
        userLoginResult.setVerifyCode(userSendCodeDTO.getCode());
        return userLoginResult;
    }
    @RequestMapping("/sendCode")
    public CommonResult<String> sendCode(String phoneNumber){
        logger.info("发送验证码的电话号码为:{}",phoneNumber);
        verificationCodeService.sendVerificationCode(phoneNumber);
        return CommonResult.success(verificationCodeService.getVerificationCode(phoneNumber));
    }
    @RequestMapping("/getCode")
    public CommonResult<String> getCode(String phoneNumber){
        logger.info("获取验证码的电话号码为:{}",phoneNumber);
        String verificationCode = verificationCodeService.getVerificationCode(phoneNumber);
        return CommonResult.success(verificationCode);
    }


    @RequestMapping("/passwordLogin")
    public CommonResult<UserLoginResult> userPasswordLogin(@Validated @RequestBody UserPasswordLoginParam param){
        logger.info("UserPasswordLoginParam:{}",JacksonUtil.writeValueAsString(param));
        UserLoginDTO login = userService.login(param);
        return CommonResult.success(convertUserLoginParam(login));
    }

    @RequestMapping("/codeLogin")
    public  CommonResult<UserLoginResult> shortMessageLogin(@Validated @RequestBody ShortMessageLoginParam param){
        logger.info("ShortMessageLoginParam:{}",JacksonUtil.writeValueAsString(param));
        UserLoginDTO login = userService.login(param);
        return CommonResult.success(convertUserLoginParam(login));

    }
    @RequestMapping("/find_list")
    public CommonResult<List<BaseUserInfoResult>> findBaseUserInfo(String identity){
        logger.info("findBaseUserInfo identity:{}", identity);
        List<UserDTO> userDTOList = userService.findUserInfo(
                UserIdentityEnum.forName(identity));
        return CommonResult.success(convertToList(userDTOList));
    }

    private List<BaseUserInfoResult> convertToList(List<UserDTO> userDTOList) {
        if(CollectionUtils.isEmpty(userDTOList)){
            return Arrays.asList();
        }
        //利用service层的写法进行转换
        List<BaseUserInfoResult> baseUserInfoResultList=userDTOList.stream().map(userDTO -> {
            BaseUserInfoResult baseUserInfoResult=new BaseUserInfoResult();
            baseUserInfoResult.setUserName(userDTO.getUserName());
            baseUserInfoResult.setIdentity(userDTO.getIdentity().name());
            baseUserInfoResult.setUserId(userDTO.getUserId());
            return baseUserInfoResult;
        }).collect(Collectors.toList());
        return baseUserInfoResultList;
    }
}
